//
//  FavoriteController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "FavoriteController.h"
#import "FavoriteModel.h"
#import "DetailViewController.h"   // video
#import "HeadLineDetailController.h"
#import "ImagesDetailController.h"
#import "ReadDetailController.h"
#import "VideoData.h"
#import "FavoriteCell.h"
#import "VideoDetailModel.h"
@interface FavoriteController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)UITableView *
myTableView;

@end

@implementation FavoriteController

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        self.dataSource = [NSMutableArray array];
    }
    return [[_dataSource retain]autorelease];
}


-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:212 / 255.0 green:25 / 255.0 blue:37 / 255.0 alpha:1];

}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self requestDataHandle];
}


-(void)initUI
{
    self.navigationItem.title = @"我的收藏";
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    [_myTableView registerClass:[FavoriteCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.myTableView];

}
#pragma mark -- tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    FavoriteModel *model = self.dataSource[indexPath.row];
    
    if ([model.flag isEqualToString:@"NEWS"]) {
        
      cell.name.text = @"【新闻】";
        cell.name.textColor = [UIColor colorWithRed:1.000 green:0.293 blue:0.268 alpha:1.000];
    }else if ([model.flag isEqualToString:@"IMAGES"]){
        cell.name.text = @"【图片】";
        cell.name.textColor = [UIColor colorWithRed:1.000 green:0.293 blue:0.268 alpha:1.000];
    }else{
        cell.name.text = @"【视频】";
        cell.name.textColor = [UIColor colorWithRed:1.000 green:0.293 blue:0.268 alpha:1.000];
    }
    cell.title.text  = model.fchtitle;
    return cell;
}

//选中后
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    FavoriteModel *model = self.dataSource[indexPath.row];
    if ([model.flag  isEqualToString:@"NEWS"]) {
        HeadLineDetailController *NewsDetail= [[HeadLineDetailController alloc] init];
        NewsDetail.docid = model.fchdocid;
        NewsDetail.newsTitle = model.fchtitle;
        NewsDetail.flag = model.fchboardid;
        [self.navigationController pushViewController:NewsDetail animated:YES];
        self.navigationController.navigationBarHidden = NO;
        [NewsDetail release];
    } if ([model.flag isEqualToString:@"IMAGES"]) {
        ImagesDetailController *imageController = [[ImagesDetailController alloc] init];
        
        imageController.skipID = model.skipID;
        imageController.skipTypeID = model.skipTypeID;
        NSLog(@"%@",model.fchdocid);
        self.navigationController.navigationBarHidden = YES;
        [self.navigationController pushViewController:imageController animated:YES];
        [imageController release];

    }
    
    
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //删除对应的单元格的数据
    FavoriteModel *model = self.dataSource[indexPath.row];
    [DataHandle deleteData:model.fchid];
    [self.dataSource removeObject:model];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
//修改cell默认删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *)indexPath{
    return @"删除";
}


-(void)requestDataHandle
{
    self.dataSource = [DataHandle getListData];
    NSLog(@"%@",self.dataSource);
}






























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
